package com.c1.client.pan;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;

/**
 * @author zhangjiangnan
 */
public class DrawUtil {
    /**
     * @param type 1,左上对齐  2,左下对齐 3,右上对齐  4,右下对齐   5,中下对齐 6,中上对齐
     */
    public static void drawRect(int x, int y, int width, int height,
                                Graphics g, String c, float alpha, int stroke, boolean solid, int type) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        if (stroke != -1) {
            g2.setStroke(new BasicStroke(stroke));
        }
        if (c != null) {
            Color color = new Color(Integer.valueOf(c, 16));
            g2.setColor(color);
        }
        if (type == 1) {
            //nothing
        }
        if (type == 2) {
            y -= height;
        }
        if (type == 3) {
            x -= width;
        }
        if (type == 4) {
            x -= width;
            y -= height;
        }
        if (type == 5) {
            x -= width / 2;
            y -= height;
        }
        if (type == 6) {
            x -= width / 2;
        }
        if (solid) {
            g2.fillRect(x, y, width, height);
        } else {
            g2.drawRect(x, y, width, height);
        }
    }

    /**
     * @param type 1,左下对齐 2,左上对齐 3,右下对齐 4,右上对齐 5,中上对齐 6,中下对齐
     * @return
     */
    public static int[] drawString(String s, Graphics g, int x, int y, String name, int style, int size, String c, int type, float alpha) {
        Font f = new Font(name, style, size);
        TextLayout tl = new TextLayout(s, f, new FontRenderContext(null, false, false));
        int width = new Double(tl.getBounds().getWidth()).intValue();
        int height = new Double(tl.getAscent() + tl.getDescent()).intValue();

        Graphics2D g2 = (Graphics2D) g;
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        if (c != null) {
            Color color = new Color(Integer.valueOf(c, 16));
            g2.setColor(color);
        }
        g2.setFont(f);
        if (type == 1) {
            g2.drawString(s, x, y);
        } else if (type == 2) {
            g2.drawString(s, x, y + height);
        } else if (type == 3) {
            g2.drawString(s, x - width, y);
        } else if (type == 4) {
            g2.drawString(s, x - width, y + height);
        } else if (type == 5) {
            g2.drawString(s, x - width / 2, y + height);
        } else if (type == 6) {
            g2.drawString(s, x - width / 2, y);
        } else {
            throw new RuntimeException();
        }

        return new int[]{width, height};
    }

    public static void drawOval(Graphics g, int x, int y, int w, String c, int stroke, boolean solid, float alpha) {
        Graphics2D g2 = (Graphics2D) g;
        if (stroke != -1) {
            g2.setStroke(new BasicStroke(stroke));
        }
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        if (c != null) {
            Color color = new Color(Integer.valueOf(c, 16));
            g2.setColor(color);
        }
        if (solid) {
            g2.fillOval(x - w, y - w, 2 * w, 2 * w);
        } else {
            g2.drawOval(x - w, y - w, 2 * w, 2 * w);
        }
    }

    public static void drawLine(Graphics g, int x, int y, int x1, int y1,
                                String c, int stroke, boolean solid, float alpha) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        if (solid) {
            if (stroke != -1) {
                g2.setStroke(new BasicStroke(stroke));
            }
        } else {
            if (stroke <= 0) {
                stroke = 1;
            }
            BasicStroke dash = new BasicStroke(stroke, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3.5f, new float[]{10, 10,}, 0f);
            g2.setStroke(dash);
        }
        if (c != null) {
            Color color = new Color(Integer.valueOf(c, 16));
            g2.setColor(color);
        }
        g2.drawLine(x, y, x1, y1);
    }
}
